using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace HappySprite.Simulation
{
    public static class ConvexHalfPlaneAlgorithm
    {
        public static bool DetectCollision(CollisionEventArgs e)
        {
            CollisionShape convex = e.This.CollisionShape;
            HalfPlane halfPlane = (HalfPlane)e.Other.CollisionShape;

            Vector2 normal = e.Other.Transform.TransformVector(halfPlane.Normal);

            Vector2 p1 = convex.SupportMapping(e.This.Transform, -normal);
            Vector2 p2 = e.Other.Transform.Position;

            float depth = Vector2.Dot(normal, p2 - p1);

            if (depth < 0f)
                return false;

            if (e.CollisionDetails == CollisionDetails.None)
                return true;

            e.Normal = -normal;
            e.Depth = depth;
            e.ContactPoint = p1;

            return true;
        }
    }
}
